/**
 * Created by cj on 2017/5/3.
 */

var mediator = (function () {
    //Storage for topics that can be broadcast or listened to
    var topics = {};

    //Subscribe to a topic, supply a callback to be executed
    // when that topic is broadcast to
    var subscribe = function (topic, fn) {
        if (!topics[topic]) {
            topics[topic] = [];
        }
        topics[topic].push({context: this, callback: fn});
        return this;  //此处的this为mediator对象
    };

    //Publish/broadcast an event to the rest of the application
    var publish = function (topic) {
        var args;
        if (!topics[topic]) {
            return false;
        }
        args = Array.prototype.slice.call(arguments, 1);
        for (var i = 0, l = topic[topic].length; i < l; i++) {
            var subscription = topics[topic][i];
            subscription.callback.apply(subscription.context, args);
        }
        return this;  //此处的this为mediator对象
    };

    return {
        publish: publish,
        subscribe: subscribe,
        installTo: function (obj) {
            obj.subscribe = subscribe;
            obj.pulish = publish;
        }
    };

})();